﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using nplJobsDAL.Select;
using nplJobsBLL.ProcessJob;
using nplJobsBLL.Common;
using nplJobsCommon.Common;
using System.Web.Security;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.UI.HtmlControls;

public partial class Job_CategoryWiseJobSearch : System.Web.UI.Page
{
    const int INITIAL_PAGER_RECORD = 20;
    PagedDataSource pagedDataSource;

    public int PageNumber
    {
        get;
        set;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!Context.User.Identity.IsAuthenticated)
        //    Response.Redirect("~/Default.aspx");

        if (!IsPostBack)
        {
            ViewState["SEARCHJOBLIST"] = null;
            PageNumber = 1;

            LoadEmployeerType();
            LoadJobCategory();
            LoadCarrerLevel();
            LoadJobType();

            this.ViewState["pageIndex"] = 0;
            LoadJobDetails();
            Page.Title = "Job search list";

        }
        UpdatePager((this.ViewState["STARTPAGENO"] == null ? 1 : Convert.ToInt32(this.ViewState["STARTPAGENO"].ToString())), (this.ViewState["PAGERRECORD"] == null ? INITIAL_PAGER_RECORD : Convert.ToInt32(this.ViewState["PAGERRECORD"].ToString())));
        SetMarkToCurrentPage(1);
        if (IsPostBack)
        {
            if (Request.Params != null)
            {
                string eventTarget = Request.Params.Get("__EVENTTARGET");
                string eventArgument = Request.Params.Get("__EVENTARGUMENT");
                if (eventArgument.Equals("blur"))
                {
                    ExecuteJobSearch();
                    UpdatePager(1, INITIAL_PAGER_RECORD);
                    SetMarkToCurrentPage(1);
                }
            }
        }
    }

    protected void chkAnyCategoryList_OnCheckedChanged(object sender, EventArgs e)
    {
        if (chkAnyCategoryList.Checked)
        {
            UncheckedAllCheckedBoxWithoutAny(chkCategoryList);
            SearchAndResetPager(sender, e);
        }
    }
    protected void chkCategoryList_OnCheckedChanged(object sender, EventArgs e)
    {
        IEnumerable<int> allChecked = chkCategoryList.Items.Cast<ListItem>()
                          .Where(i => i.Selected)
                          .Select(i => int.Parse(i.Value));
        int countChk = allChecked.Count();
        if (countChk > 0)
        {
            chkAnyCategoryList.Checked = false;
        }
        else if (countChk == 0)
            chkAnyCategoryList.Checked = true;

        SearchAndResetPager(sender, e);
    }
    protected void chkAnyIndustryList_OnCheckedChanged(object sender, EventArgs e)
    {
        if (chkAnyIndustryList.Checked)
        {
            UncheckedAllCheckedBoxWithoutAny(chkIndustryList);
            SearchAndResetPager(sender, e);
        }
    }
    protected void chkIndustryList_OnCheckedChanged(object sender, EventArgs e)
    {
        IEnumerable<int> allChecked = chkIndustryList.Items.Cast<ListItem>()
                          .Where(i => i.Selected)
                          .Select(i => int.Parse(i.Value));
        int countChk = allChecked.Count();
        if (countChk > 0)
        {
            chkAnyIndustryList.Checked = false;
        }
        else if (countChk == 0)
            chkAnyIndustryList.Checked = true;

        SearchAndResetPager(sender, e);
    }

    protected void chkAnyCareerLevelList_OnCheckedChanged(object sender, EventArgs e)
    {
        if (chkAnyCareerLevelList.Checked)
        {
            UncheckedAllCheckedBoxWithoutAny(chkCareerLevelList);
            SearchAndResetPager(sender, e);
        }
    }
    protected void chkCareerLevelList_OnCheckedChanged(object sender, EventArgs e)
    {
        IEnumerable<int> allChecked = chkCareerLevelList.Items.Cast<ListItem>()
                          .Where(i => i.Selected)
                          .Select(i => int.Parse(i.Value));
        int countChk = allChecked.Count();
        if (countChk > 0)
        {
            chkAnyCareerLevelList.Checked = false;
        }
        else if (countChk == 0)
            chkAnyCareerLevelList.Checked = true;

        SearchAndResetPager(sender, e);
    }

    protected void chkAnyJobTypeList_OnCheckedChanged(object sender, EventArgs e)
    {
        if (chkAnyJobTypeList.Checked)
        {
            UncheckedAllCheckedBoxWithoutAny(chkJobTypeList);
            SearchAndResetPager(sender, e);
        }
    }
    protected void chkJobTypeList_OnCheckedChanged(object sender, EventArgs e)
    {
        IEnumerable<int> allChecked = chkJobTypeList.Items.Cast<ListItem>()
                          .Where(i => i.Selected)
                          .Select(i => int.Parse(i.Value));
        int countChk = allChecked.Count();
        if (countChk > 0)
        {
            chkAnyJobTypeList.Checked = false;
        }
        else if (countChk == 0)
            chkAnyJobTypeList.Checked = true;

        SearchAndResetPager(sender, e);
    }

    protected void chkClosingDateAny_OnCheckedChanged(object sender, EventArgs e)
    {
        chkClosingDateAny.Checked = true;
        if (chkClosingDateAny.Checked)
        {
            CheckboxClosingDate(false);
        }
        SearchAndResetPager(sender, e);
    }
    protected void chkClosingDateToday_OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = sender as CheckBox;
        if (chk != null && chk.Checked)
        {
            chkClosingDateAny.Checked = false;
        }
        else
        {
            if (chkClosingDateToday.Checked || chkClosingDateTomorrow.Checked || chkClosingDateNext5Days.Checked)
                chkClosingDateAny.Checked = false;
            else
                chkClosingDateAny.Checked = true;
        }
        SearchAndResetPager(sender, e);
    }

    protected void chkAnyTime_OnCheckedChanged(object sender, EventArgs e)
    {
        chkAnyTime.Checked = true;
        if (chkAnyTime.Checked)
        {
            chkYesterday.Checked = false;
            chkTwoToSevenDays.Checked = false;
            chkEightToForteenDays.Checked = false;
            chkFifteenToThirtyDays.Checked = false;
        }
        SearchAndResetPager(sender, e);
    }

    protected void chkYesterday_OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = sender as CheckBox;
        if (chk != null && chk.Checked)
        {
            chkAnyTime.Checked = false;
        }
        else
        {
            if (chkYesterday.Checked || chkTwoToSevenDays.Checked || chkYesterday.Checked || chkFifteenToThirtyDays.Checked)
                chkAnyTime.Checked = false;
            else
                chkAnyTime.Checked = true;
        }
        SearchAndResetPager(sender, e);
    }

    protected void btnSearch_OnClick(object sender, EventArgs e)
    {
        this.ViewState["pageIndex"] = 0;
        ExecuteJobSearch();
    }

    protected void ddlPage_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        DisplayLoadedJobsIntoGrid(SortType.Relivance);
        UpdatePager(1, INITIAL_PAGER_RECORD);
        SetMarkToCurrentPage(1);
    }

    protected void link_Click(object sender, EventArgs e)
    {
        this.ViewState["pageIndex"] = Convert.ToInt32(((LinkButton)sender).Text) - 1;
        ExecuteJobSearch();

        UpdatePager((this.ViewState["STARTPAGENO"] == null ? 1 : Convert.ToInt32(this.ViewState["STARTPAGENO"].ToString())), (this.ViewState["PAGERRECORD"] == null ? INITIAL_PAGER_RECORD : Convert.ToInt32(this.ViewState["PAGERRECORD"].ToString())));
        int currentPage = Convert.ToInt32(((LinkButton)sender).Text);
        SetMarkToCurrentPage(currentPage);
    }

    protected void linkNext_Click(object sender, EventArgs e)
    {
        int pagerRecord = PageLinks.Controls.Count;
        int startPageNo = (this.ViewState["STARTPAGENO"] == null ? 1 : Convert.ToInt32(this.ViewState["STARTPAGENO"].ToString())) + pagerRecord;
        pagerRecord = (this.ViewState["PAGERRECORD"] == null ? pagerRecord : Convert.ToInt32(this.ViewState["PAGERRECORD"].ToString())) + pagerRecord;

        this.ViewState["STARTPAGENO"] = startPageNo;
        this.ViewState["PAGERRECORD"] = pagerRecord;

        UpdatePager(startPageNo, pagerRecord);
    }

    protected void linkPrevious_Click(object sender, EventArgs e)
    {
        int startPageNo = (this.ViewState["STARTPAGENO"] == null ? 1 : Convert.ToInt32(this.ViewState["STARTPAGENO"].ToString())) - INITIAL_PAGER_RECORD;
        int pagerRecord = (this.ViewState["PAGERRECORD"] == null ? INITIAL_PAGER_RECORD : Convert.ToInt32(this.ViewState["PAGERRECORD"].ToString())) - INITIAL_PAGER_RECORD;

        this.ViewState["STARTPAGENO"] = startPageNo;
        this.ViewState["PAGERRECORD"] = pagerRecord;

        UpdatePager(startPageNo, pagerRecord);
    }

    protected void SortMenu_OnClick(object sender, EventArgs e)
    {
        LinkButton lnkMenu = (LinkButton)sender;
        if (lnkMenu != null)
        {
            this.ViewState["pageIndex"] = 0;
            MadeSortJobs(lnkMenu.Text);
            UpdatePager(1, INITIAL_PAGER_RECORD);
            SetMarkToCurrentPage(1);
        }
    }

    private void CheckboxClosingDate(bool b)
    {
        chkClosingDateToday.Checked = b;
        chkClosingDateTomorrow.Checked = b;
        chkClosingDateNext5Days.Checked = b;
    }

    private void SearchAndResetPager(object sender, EventArgs e)
    {
        btnSearch_OnClick(sender, e);
        UpdatePager(1, INITIAL_PAGER_RECORD);
        SetMarkToCurrentPage(1);
    }

    private void UncheckedAllCheckedBoxWithoutAny(CheckBoxList chkboxlist)
    {
        for (int i = 0; i < chkboxlist.Items.Count; i++)
        {
            chkboxlist.Items[i].Selected = false;
        }
    }

    private void MadeSortJobs(string sortText)
    {
        if (sortText.Equals("Relevance"))
        {
            DisplayLoadedJobsIntoGrid(SortType.Relivance);
        }
        else if (sortText.Equals("Title"))
        {
            DisplayLoadedJobsIntoGrid(SortType.Title);
        }
        else if (sortText.Equals("Location"))
        {
            DisplayLoadedJobsIntoGrid(SortType.Location);
        }
        else if (sortText.Equals("Salary"))
        {
            DisplayLoadedJobsIntoGrid(SortType.Salary);
        }
        else if (sortText.Equals("Advertiser"))
        {
            DisplayLoadedJobsIntoGrid(SortType.Advertiser);
        }
        else if (sortText.Equals("Date"))
        {
            DisplayLoadedJobsIntoGrid(SortType.Date);
        }
        else if (sortText.Equals("Closing Soon"))
        {
            DisplayLoadedJobsIntoGrid(SortType.ClosingSoon);
        }
    }

    private void LoadJobDetails()
    {
        if (Session["QUICKlINKSTYPE"] != null)
        {
            SetAnyCheckboxStatus(true);

            string quickLinksType = Session["QUICKlINKSTYPE"].ToString();
            if (quickLinksType.Equals("0"))
            {
                chkClosingDateAny.Checked = false;
                chkClosingDateTomorrow.Checked = true;
                LoadCategoryWiseJobs("", "", "", "", "", "", "", "", "2");
            }
            else
            {
                LoadCategoryWiseJobs("", "", "", "", quickLinksType, "", "", "", "");

                chkJobTypeList.SelectedValue = quickLinksType;
                chkAnyJobTypeList.Checked = false;
            }
        }
        else if (Session["CATEGORYLINKSTYPE"] != null)
        {
            string categoryId = Session["CATEGORYLINKSTYPE"].ToString();
            if (categoryId.Equals("99"))
                LoadOverseasJobs();
            else
            {
                SetAnyCheckboxStatus(true);
                if (Session["GOVTJOBS"] != null && Convert.ToBoolean(Session["GOVTJOBS"].ToString()))
                {
                    LoadGovtCategoryWiseJobs("", "", "", "", "", "", categoryId, "", "");
                    chkCategoryList.SelectedValue = "34";
                    chkAnyCategoryList.Checked = false;
                }
                else if (Session["NEWSPAPERJOBS"] != null && Convert.ToBoolean(Session["NEWSPAPERJOBS"].ToString()))
                {
                    chkCategoryList.SelectedValue = "55";
                    chkAnyCategoryList.Checked = false;
                    LoadNewspaperCategoryWiseJobs("", "", "", "", "", "", categoryId, "55", "");
                }
                else
                {
                    LoadCategoryWiseJobs("", "", "", "", "", "", categoryId, "", "");
                    chkIndustryList.SelectedValue = categoryId;
                    chkAnyIndustryList.Checked = false;
                }
            }
        }
        else if (Session["JOBTYPE"] != null)
        {
            SetAnyCheckboxStatus(true);
            string jobType = Session["JOBTYPE"].ToString();
            LoadCategoryWiseJobs("", "", "", "", jobType, "", "", "", "");
            chkJobTypeList.SelectedValue = jobType;
            chkAnyJobTypeList.Checked = false;
        }
        else if (Session["LOCATIONWISE"] != null)
        {
            SetAnyCheckboxStatus(true);

            int location = Convert.ToInt32(Session["LOCATIONWISE"].ToString());
            txtWhere.Text = Session["LOCATIONWISENAME"] == null ? string.Empty : Session["LOCATIONWISENAME"].ToString();
            LoadLocationWiseJobs(location);
        }
        else
        {
            SetAnyCheckboxStatus(true);

            string what = Session["what"] == null ? txtWhat.Text : Session["what"].ToString();
            string where = Session["where"] == null ? txtWhere.Text : Session["where"].ToString();
            txtWhat.Text = what;
            txtWhere.Text = where;
            LoadCategoryWiseJobs(what, where, "", "", "", "", "", "", "");
        }
    }

    private void SetAnyCheckboxStatus(bool b)
    {
        chkAnyCategoryList.Checked = b;
        chkAnyIndustryList.Checked = b;
        chkAnyCareerLevelList.Checked = b;
        chkAnyJobTypeList.Checked = b;
        chkAnyTime.Checked = b;
        chkClosingDateAny.Checked = b;
    }

    private void ExecuteJobSearch()
    {
        SetSessionNULL();
        string posted = GetJobPostedList();
        string closing = GetJobClosingDateList();
        string jobType = GetJobTypeList();
        string careerLevel = GetCareerLevelList();
        string industry = GetIndustryList();
        string category = GetCategoryGeneralList();
        LoadCategoryWiseJobs(txtWhat.Text, txtWhere.Text, closing, posted, jobType, careerLevel, industry, category, "");
    }

    private string GetJobClosingDateList()
    {
        string closing = "";
        if (chkClosingDateAny.Checked)
            closing = closing + "0";

        if (chkClosingDateToday.Checked)
            closing = string.IsNullOrEmpty(closing.Trim()) ? "1" : closing + ",1";

        if (chkClosingDateTomorrow.Checked)
            closing = string.IsNullOrEmpty(closing.Trim()) ? "2" : closing + ",2";

        if (chkClosingDateNext5Days.Checked)
            closing = string.IsNullOrEmpty(closing.Trim()) ? "3" : closing + ",3";

        return closing.Trim();
    }

    private string GetJobPostedList()
    {
        string posted = "";
        if (chkAnyTime.Checked)
            posted = posted + "0";

        if (chkYesterday.Checked)
            posted = string.IsNullOrEmpty(posted.Trim()) ? "1" : posted + ",1";

        if (chkTwoToSevenDays.Checked)
            posted = string.IsNullOrEmpty(posted.Trim()) ? "2" : posted + ",2";

        if (chkEightToForteenDays.Checked)
            posted = string.IsNullOrEmpty(posted.Trim()) ? "3" : posted + ",3";

        if (chkFifteenToThirtyDays.Checked)
            posted = string.IsNullOrEmpty(posted.Trim()) ? "4" : posted + ",4";

        return posted.Trim();
    }

    private string GetJobTypeList()
    {
        string jobType = "";

        if (chkAnyJobTypeList.Checked)
        {
            return jobType;
        }

        for (int i = 0; i < chkJobTypeList.Items.Count; i++)
        {
            if (chkJobTypeList.Items[i].Selected)
            {
                jobType = jobType + "," + chkJobTypeList.Items[i].Value;
            }
        }
        if (jobType.Trim().Length == 0)
            return jobType;
        else
            return jobType.Trim().Substring(1);
    }

    private string GetCareerLevelList()
    {
        string careerLevel = "";
        if (chkCareerLevelList.Items[0].Selected)
            careerLevel = careerLevel + ",1";

        if (chkCareerLevelList.Items[1].Selected)
            careerLevel = careerLevel + ",2";

        if (chkCareerLevelList.Items[2].Selected)
            careerLevel = careerLevel + ",3";

        return careerLevel.Trim().Length == 0 ? careerLevel.Trim() : careerLevel.Trim().Substring(1);
    }

    private string GetIndustryList()
    {
        string industry = "";

        if (chkAnyIndustryList.Checked)
        {
            return industry;
        }

        for (int i = 0; i < chkIndustryList.Items.Count; i++)
        {
            if (chkIndustryList.Items[i].Selected)
            {
                industry = industry + "," + chkIndustryList.Items[i].Value;
            }
        }
        if (industry.Trim().Length == 0)
            return industry;
        else
            return industry.Trim().Substring(1);
    }

    private string GetCategoryGeneralList()
    {
        string category = "";

        if (chkAnyCategoryList.Checked)
        {
            return category;
        }

        for (int i = 0; i < chkCategoryList.Items.Count; i++)
        {
            if (chkCategoryList.Items[i].Selected)
            {
                category = category + "," + chkCategoryList.Items[i].Value;
            }
        }
        if (category.Trim().Length == 0)
            return category;
        else
            return category.Trim().Substring(1);
    }

    private void SessionClear()
    {
        Session["what"] = null;
        Session["where"] = null;
    }

    public void LoadEmployeerType()
    {
        try
        {
            ProcessEmployeerType employeertype = new ProcessEmployeerType();
            employeertype.GetEmployeerTypeWithJobCount();

            chkCategoryList.DataSource = employeertype.EmployeerType.OrderBy(od => od.ID).ToList();
            chkCategoryList.DataTextField = "Type";
            chkCategoryList.DataValueField = "ID";
            chkCategoryList.DataBind();
        }
        catch (Exception exp)
        {
            throw new Exception(exp.Message.ToString());
        }
    }

    public void LoadJobCategory()
    {
        try
        {
            ProcessJobCategory category = new ProcessJobCategory();
            category.GetCategoryWithJobCount();

            chkIndustryList.DataSource = category.JobCategory.OrderBy(od => od.ID).ToList();
            chkIndustryList.DataTextField = "Caterogy";
            chkIndustryList.DataValueField = "ID";
            chkIndustryList.DataBind();
        }
        catch (Exception exp)
        {
            throw new Exception(exp.Message.ToString());
        }
    }

    private void LoadCarrerLevel()
    {
        List<CareerLevel> lstCareerLevel = new SelectCareerLevel().GetCareerLevel();
        if (lstCareerLevel.Count > 0 && lstCareerLevel[0].ID == 0)
            lstCareerLevel.RemoveAt(0);
        chkCareerLevelList.DataSource = lstCareerLevel;

        chkCareerLevelList.DataTextField = "Level";
        chkCareerLevelList.DataValueField = "ID";
        chkCareerLevelList.DataBind();
    }

    private void LoadJobType()
    {
        List<JobType> lstJobType = new SelectJobType().GetJobType();
        if (lstJobType.Count > 0 && lstJobType[0].ID == 0)
            lstJobType.RemoveAt(0);
        chkJobTypeList.DataSource = lstJobType;
        chkJobTypeList.DataTextField = "Type";
        chkJobTypeList.DataValueField = "ID";
        chkJobTypeList.DataBind();
    }

    private void LoadCategoryWiseJobs(string what, string where, string closing, string posted, string jobType, string careerLevel, string industry, string category, string deadlineTomorrow)
    {
        List<Job> jobList = SelectJobList.GetCategoryWiseJobDetails(what, where, closing, posted, jobType, careerLevel, industry, category, deadlineTomorrow);

        string sortType = sortTypeVal.Value;
        if (sortType == "0")
        {
            jobList = jobList.OrderByDescending(ob => ob.PostedDate).ToList();
        }
        else if (sortType == "1") //Salary
        {
            List<Job> lstJobTemp = new List<Job>();
            lstJobTemp.AddRange(jobList.Where(w => w.SalaryRange == 2).OrderByDescending(o => o.SalaryRangeMax).ToList()); //want to display following range
            lstJobTemp.AddRange(jobList.Where(w => w.SalaryRange != 2).OrderBy(o => o.SalaryRange).ToList()); //Negotiatable/do not want to mention
            jobList = lstJobTemp;
        }
        else if (sortType == "2") //Advertiser
        {
            jobList = jobList.OrderBy(ob => ob.CompanyName).ToList();
        }
        else if (sortType == "3") //Date
        {
            jobList = jobList.OrderByDescending(ob => ob.PostedDate).ToList();
        }
        else if (sortType == "4")
        {
            jobList = jobList.OrderBy(ob => ob.Deadline).ToList();
        }

        FillJobIntoGrid(jobList);
        ViewState["SEARCHJOBLIST"] = jobList;
        lblTotalJobs.Text = jobList.Count.ToString();
    }

    private void LoadGovtCategoryWiseJobs(string what, string where, string closing, string posted, string jobType, string careerLevel, string industry, string category, string deadlineTomorrow)
    {
        List<Job> jobList = SelectJobList.GetCategoryWiseJobDetails(what, where, closing, posted, jobType, careerLevel, industry, category, deadlineTomorrow);
        jobList = jobList.Where(w => w.EmployeerTypeID == 34).ToList();
        FillJobIntoGrid(jobList);
        ViewState["SEARCHJOBLIST"] = jobList;
        lblTotalJobs.Text = jobList.Count.ToString();
    }

    private void LoadNewspaperCategoryWiseJobs(string what, string where, string closing, string posted, string jobType, string careerLevel, string industry, string category, string deadlineTomorrow)
    {
        List<Job> jobList = SelectJobList.GetCategoryWiseJobDetails(what, where, closing, posted, jobType, careerLevel, industry, category, deadlineTomorrow);
        //jobList = jobList.Where(w => w.EmployeerTypeID == 55).ToList();
        FillJobIntoGrid(jobList);
        ViewState["SEARCHJOBLIST"] = jobList;
        lblTotalJobs.Text = jobList.Count.ToString();
    }

    private void LoadLocationWiseJobs(int location)
    {
        List<Job> jobList = SelectJobList.GetLocationWiseJobDetails(location);
        FillJobIntoGrid(jobList);
        ViewState["SEARCHJOBLIST"] = jobList;
    }

    private void LoadOverseasJobs()
    {
        List<Job> jobList = SelectJobList.GetOverseasJobDetails();
        FillJobIntoGrid(jobList);
        lblTotalJobs.Text = jobList.Count.ToString();
    }

    private static void SetSessionNULL()
    {
        HttpContext.Current.Session["QUICKlINKSTYPE"] = null;
        HttpContext.Current.Session["CATEGORYLINKSTYPE"] = null;
        HttpContext.Current.Session["GOVTJOBS"] = null;
        HttpContext.Current.Session["NEWSPAPERJOBS"] = null;
        HttpContext.Current.Session["JOBTYPE"] = null;
        HttpContext.Current.Session["LOCATIONWISE"] = null;
    }

    private void DisplayLoadedJobsIntoGrid(SortType sortType)
    {
        if (ViewState["SEARCHJOBLIST"] != null)
        {
            List<Job> jobList = ViewState["SEARCHJOBLIST"] as List<Job>;
            if (jobList != null)
            {
                if (sortType == SortType.Relivance)
                {
                    jobList = jobList.OrderByDescending(ob => ob.PostedDate).ToList();
                }
                else if (sortType == SortType.Title)
                {
                    jobList = jobList.OrderBy(ob => ob.JobTitle).ToList();
                }
                else if (sortType == SortType.Location)
                {
                    jobList = jobList.OrderBy(ob => ob.Location).ToList();
                }
                else if (sortType == SortType.Salary)
                {
                    List<Job> lstJobTemp = new List<Job>();
                    lstJobTemp.AddRange(jobList.Where(w => w.SalaryRange == 2).OrderByDescending(o => o.SalaryRangeMax).ToList()); //want to display following range
                    lstJobTemp.AddRange(jobList.Where(w => w.SalaryRange != 2).OrderBy(o => o.SalaryRange).ToList()); //Negotiatable/do not want to mention

                    //jobList.Clear();
                    jobList = lstJobTemp;
                }
                else if (sortType == SortType.Advertiser)
                {
                    jobList = jobList.OrderBy(ob => ob.CompanyName).ToList();
                }
                else if (sortType == SortType.Date)
                {
                    jobList = jobList.OrderByDescending(ob => ob.PostedDate).ToList();
                }
                else if (sortType == SortType.ClosingSoon)
                {
                    jobList = jobList.OrderBy(ob => ob.Deadline).ToList();
                }

                FillJobIntoGrid(jobList);
            }
        }
    }

    private void FillJobIntoGrid(List<Job> jobList)
    {
        pagedDataSource = new PagedDataSource();
        pagedDataSource.DataSource = jobList;
        pagedDataSource.PageSize = Convert.ToInt32(ddlPage.SelectedValue);
        pagedDataSource.AllowPaging = true;
        pagedDataSource.CurrentPageIndex = (int)this.ViewState["pageIndex"];

        this.ViewState["PageCount"] = pagedDataSource.PageCount;

        gvJobsList.DataSource = pagedDataSource;
        gvJobsList.DataBind();
    }

    private void UpdatePager(int startPageNo, int pagerRecord)
    {
        int pageCount = Convert.ToInt32(this.ViewState["PageCount"].ToString());

        PageLinksPrevious.Controls.Clear();
        {
            LinkButton linkPrevious = new LinkButton();
            linkPrevious.ID = "linkPrevious";
            linkPrevious.Text = "&laquo;";
            linkPrevious.Click += new EventHandler(linkPrevious_Click);
            PageLinksPrevious.Controls.Add(linkPrevious);
        }
        PageLinksNext.Controls.Clear();
        {
            LinkButton linkNext = new LinkButton();
            linkNext.ID = "linkNext";
            linkNext.Text = "&raquo;";
            linkNext.Click += new EventHandler(linkNext_Click);
            PageLinksNext.Controls.Add(linkNext);
        }

        if (pagerRecord > pageCount)
        {
            pagerRecord = pageCount;
        }

        PageLinks.Controls.Clear();
        for (int i = startPageNo; i <= pagerRecord; i++)
        {
            HtmlGenericControl li = new HtmlGenericControl("li");

            LinkButton link = new LinkButton();
            link.CssClass = "page-box";
            link.ID = link.Text = i.ToString();
            link.Click += new EventHandler(link_Click);
            li.Controls.Add(link);

            PageLinks.Controls.Add(link);
        }

        if (startPageNo.Equals(1))
            PageLinksPrevious.Controls[0].Visible = false;

        if (pagerRecord.Equals(pageCount))
            PageLinksNext.Controls[0].Visible = false;
    }

    private void SetMarkToCurrentPage(int currentPage)
    {
        int totalPager = PageLinks.Controls.Count;
        for (int i = 0; i < totalPager; i++)
        {
            LinkButton link = (LinkButton)PageLinks.Controls[i];
            if (link != null)
            {
                link.CssClass = "page-box";

                if (link.Text == currentPage.ToString())
                {
                    link.CssClass = "page-box page-box-selection";
                }
            }
        }
        {
            int toNo = currentPage * Convert.ToInt32(ddlPage.SelectedValue);
            int totalNo = Convert.ToInt32(lblTotalJobs.Text);
            int startNo = ((currentPage * Convert.ToInt32(ddlPage.SelectedValue))) - ((Convert.ToInt32(ddlPage.SelectedValue)) - 1);
            int endNo = (toNo > totalNo ? totalNo : toNo);
            lblPagingFromTo.Text = string.Format("{0} - {1}", endNo < 1 ? 0 : startNo, endNo);
        }
    }

    private int GetClosingDateCheckedCount()
    {
        List<CheckBox> lstCheckBox = new List<CheckBox>();
        lstCheckBox.Add(chkClosingDateAny);
        lstCheckBox.Add(chkClosingDateToday);
        lstCheckBox.Add(chkClosingDateTomorrow);
        lstCheckBox.Add(chkClosingDateNext5Days);

        IEnumerable<string> allChecked = lstCheckBox.Cast<CheckBox>()
                      .Where(i => i.Checked && !i.Text.Equals("Any"))
                      .Select(i => i.Text);

        return allChecked.Count();
    }

    private int GetPostedOnCheckedCount()
    {
        List<CheckBox> lstCheckBox = new List<CheckBox>();
        lstCheckBox.Add(chkAnyTime);
        lstCheckBox.Add(chkYesterday);
        lstCheckBox.Add(chkTwoToSevenDays);
        lstCheckBox.Add(chkEightToForteenDays);
        lstCheckBox.Add(chkFifteenToThirtyDays);

        IEnumerable<string> allChecked = lstCheckBox.Cast<CheckBox>()
                      .Where(i => i.Checked && !i.Text.Equals("Any"))
                      .Select(i => i.Text);

        return allChecked.Count();
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static List<string> GetAutoCompleteWhatData(string txt)
    {
        ProcessWhatWhereData ProcessWhatWhereData = new ProcessWhatWhereData();
        ProcessWhatWhereData.txt = txt.Trim();
        ProcessWhatWhereData.GetWhatData();
        return ProcessWhatWhereData.Data;
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static List<string> GetAutoCompleteWhereData(string txt)
    {
        ProcessWhatWhereData ProcessWhatWhereData = new ProcessWhatWhereData();
        ProcessWhatWhereData.txt = txt;
        ProcessWhatWhereData.GetWhereData();
        return ProcessWhatWhereData.Data;
    }

    private enum SortType
    {
        Relivance,
        Title,
        Location,
        Salary,
        Advertiser,
        Date,
        ClosingSoon
    }
}